package com.central.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.utils.ConvertUtils;
import com.central.modules.security.password.PasswordUtils;
import com.central.modules.sys.dao.SysUserDao;
import com.central.modules.sys.entity.SysUserEntity;
import com.central.modules.sys.enums.SuperAdminEnum;
import com.central.modules.sys.enums.UserStatusEnum;
import com.central.modules.sys.model.AddSysUserRequest;
import com.central.modules.sys.model.SysUserQueryBean;
import com.central.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 系统用户
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Override
    public IPage<SysUserEntity> pageSearch(SysUserQueryBean queryBean) {
        Page<SysUserEntity> page = new Page<>(queryBean.getPageStart(), queryBean.getPageSize());
        LambdaQueryWrapper<SysUserEntity> queryWrapper = new LambdaQueryWrapper<SysUserEntity>();
        queryWrapper.orderByDesc(SysUserEntity::getCreateDate);
        if (StringUtils.isNotBlank(queryBean.getUsername())) {
            queryWrapper.eq(SysUserEntity::getUsername, queryBean.getUsername());
        }

        if (Objects.nonNull(queryBean.getRoleId())) {
            queryWrapper.inSql(SysUserEntity::getId,"select  user_id from sys_role_user where role_id = " + queryBean.getRoleId());
        }
        return this.page(page, queryWrapper);
    }


    @Override
    public List<SysUserEntity> list(SysUserQueryBean queryBean) {
        LambdaQueryWrapper<SysUserEntity> queryWrapper = new LambdaQueryWrapper<SysUserEntity>();
        if (StringUtils.isNotBlank(queryBean.getUsername())) {
            queryWrapper.eq(SysUserEntity::getUsername, queryBean.getUsername());
        }
        return this.list(queryWrapper);
    }

    @Override
    public SysUserEntity getDetailById(Long id) {
        return this.getById(id);
    }

    @Override
    public SysUserEntity getByUsername(String username) {
        LambdaQueryWrapper<SysUserEntity> queryWrapper = new LambdaQueryWrapper<SysUserEntity>();
        queryWrapper.eq(SysUserEntity::getUsername, username);
        return this.getOne(queryWrapper);
    }

    @Override
    public void add(AddSysUserRequest request) {
        SysUserEntity entity = ConvertUtils.sourceToTarget(request, SysUserEntity.class);
        //密码加密
        String password = PasswordUtils.encode(entity.getPassword());
        entity.setPassword(password);
        //保存用户
        entity.setSuperAdmin(Integer.valueOf(SuperAdminEnum.NO.getCode()));
        entity.setStatus(Integer.valueOf(UserStatusEnum.DISABLE.getCode()));
        entity.setUsername(request.getMobile());
        this.save(entity);
    }

    @Override
    public void updateById(Long id, AddSysUserRequest request) {
        UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<SysUserEntity>();
        updateWrapper.lambda()
                .eq(SysUserEntity::getId, id)
                .set(SysUserEntity::getRealName, request.getRealName())
                .set(SysUserEntity::getGender, request.getGender())
                .set(SysUserEntity::getHeadUrl, request.getHeadUrl())
                .set(SysUserEntity::getUpdateDate, new Date())
        ;
        this.update(updateWrapper);

    }

    @Override
    public void delete(Long[] ids) {
        //删除用户
        this.removeBatchByIds(Arrays.asList(ids));

    }

    @Override
    public void updatePassword(Long id, String newPassword) {
        newPassword = PasswordUtils.encode(newPassword);
        UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<SysUserEntity>();
        updateWrapper.lambda()
                .eq(SysUserEntity::getId, id).set(SysUserEntity::getPassword, newPassword);
        this.update(new SysUserEntity(), updateWrapper);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<SysUserEntity>();
        updateWrapper.lambda()
                .eq(SysUserEntity::getId, id).set(SysUserEntity::getStatus, status);
        this.update( new SysUserEntity(), updateWrapper);
    }
}
